package com.sgb.kjwl.view.ui.activity.self.publish;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarPublish1Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.CarBrandEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddCarAddressEntity3;
import com.sgb.kjwl.model.entity.user.AddCarEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.CommonPickerEntity3;
import com.sgb.kjwl.model.entity.user.ServiceEntity;
import com.sgb.kjwl.utils.EditInputFilter;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LeaseCarBrandEvent;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.kjwl.view.ui.activity.house.CarBrandActivity;
import com.sgb.kjwl.view.ui.adapter.MultiDictAdapter4;
import com.sgb.kjwl.view.ui.adapter.self.publish.ImageAdapter;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.view.widget.TimeRangePopWindow4;
import com.sgb.kjwl.viewmodel.self.publish.CarPublishModel1;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

/**
 * 车辆发布步骤1
 */
public class CarPublishActivity1 extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    private ActivityCarPublish1Binding binding;

    private Thread thread;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private static final int MSG_LOAD_FAILED = 0x0003;
    private static final int MSG_LOAD_DATA_BACK = 0x0004;
    private static final int MSG_LOAD_BACK_SUCCESS = 0x0005;

    @Inject
    CarPublishModel1 viewModel;
    private int carPosition;
    private List<CommonPickerEntity3> carPositionList;
    private int typePosition;
    private List<CommonPickerEntity3> typeEntityList;
    private int brandPosition;
    private List<CommonPickerEntity3> brandEntityList;
    private int yearPosition;
    private List<CommonPickerEntity3> yearEntityList;
    private int numPosition;
    private List<CommonPickerEntity3> numEntityList;
    private int speedPosition;
    private List<CommonPickerEntity3> speedEntityList;
    private int configPosition;
    private List<CommonPickerEntity3> configEntityList;
    private int fuelPosition;
    private List<CommonPickerEntity3> fuelList;
    private int drivePosition;
    private List<CommonPickerEntity3> driveEntityList;
    private int windowPosition;
    private List<CommonPickerEntity3> windowEntityList;
    private int pdcPosition;
    private List<CommonPickerEntity3> pdcEntityList;

    private int rentPosition;
    private List<CommonPickerEntity3> rentEntityList;
    private List<PickSelectEntity> invoiceDatas, invoiceGGDatas;
    private List<PickSelectEntity> tariffDatas = new ArrayList<>();

    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> pickupEntityList;
    private final List<List<AddressEntity<List<AddressEntity>>>> pickupOpt2 = new ArrayList<>();
    private final List<List<List<AddressEntity>>> pickupOpt3 = new ArrayList<>();
    private int pickupCityPosition1;
    private int pickupCityPosition2;
    private int pickupCityPosition3;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> backEntityList;
    private final List<List<AddressEntity<List<AddressEntity>>>> backOpt2 = new ArrayList<>();
    private final List<List<List<AddressEntity>>> backOpt3 = new ArrayList<>();
    private int backCityPosition1;
    private int backCityPosition2;
    private int backCityPosition3;

    private boolean isLoaded = false;
    private boolean backLoad = false;
    private List<ServiceEntity> serviceEntities;
    private final AddCarEntity3 addCarEntity3 = new AddCarEntity3();
    private MapLongAndLatEntity pickupLongLat;
    private MapLongAndLatEntity returnLongLat;

    private MultiDictAdapter4 dictAdapter;
    private final List<String> additionIds = new ArrayList<>();
    private boolean isApprove = false;//是否需要审批

    private List<PickSelectEntity> departmentData = new ArrayList<>();
    private List<LocalMedia> selectList = new ArrayList<>();
    private List<File> fileList;
    private List<String> urls;
    public int imgTotal;
    private int currentImgNum;
    private final List<String> uploadImgList = new ArrayList<>();
    private ImageAdapter adapter;
    private String id;
    private String updateUserNo = "";
    private boolean isCompany = false;//是否企业账户
    private String myCompanyNo;

    public static void start(Context context, String id) {
        Intent intent = new Intent();
        intent.setClass(context, CarPublishActivity1.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, CarPublishActivity1.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置沉浸式状态栏的上移问题
        setTileImmerse(true);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(CarPublishActivity1.this, R.layout.activity_car_publish1);
        binding.setViewModel(viewModel);
        binding.topLayout.titleView.setText("车辆出租发布");
        binding.topLayout.ivBack.setOnClickListener(this);
        binding.include.tvUse.setOnClickListener(this);
        binding.btnSave.setOnClickListener(this);
        binding.btnSubmit.setOnClickListener(this);
        binding.itemTvOut.setOnClickListener(this);
        binding.itemTvType.setOnWholeItemClickListener(this);
        binding.itemTvBrand.setOnWholeItemClickListener(this);
        binding.itemTvYearDate.setOnWholeItemClickListener(this);
        binding.itemTvNum.setOnWholeItemClickListener(this);
        binding.itemTvSpeed.setOnWholeItemClickListener(this);
        binding.itemTvConfig.setOnWholeItemClickListener(this);
        binding.itemTvFuel.setOnWholeItemClickListener(this);
        binding.itemTvDriver.setOnWholeItemClickListener(this);
        binding.itemTvWindow.setOnWholeItemClickListener(this);
        binding.itemTvPdc.setOnWholeItemClickListener(this);
        binding.itemTvDepartmentName.setOnWholeItemClickListener(this);
        binding.itemTvInvoice.setOnWholeItemClickListener(this);
        binding.itemTvInvoiceGg.setOnWholeItemClickListener(this);
        binding.itemTvSl.setOnWholeItemClickListener(this);
        binding.itemTvServicer.setOnWholeItemClickListener(this);
        binding.itemTvAllowTime.setOnWholeItemClickListener(this);
        binding.itemTvGetcararea.setOnWholeItemClickListener(this);
        binding.itemTvBackcararea.setOnWholeItemClickListener(this);
        binding.itemTvGetcaraddress.setOnWholeItemClickListener(this);
        binding.itemTvBackcaraddress.setOnWholeItemClickListener(this);
        binding.itemTvPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        binding.itemTvMoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvMoney.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.itemTvPay.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvPay.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.itemTvWeek.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvWeek.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.itemTvMonth.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvMonth.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.itemTvYear.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvYear.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.itemTvOutContent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvOutContent.setFilters(new EditInputFilter[]{new EditInputFilter(99999999.99, 2)});
        binding.itemTvCapacity.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
        binding.itemTvCapacity.setMaxSize(3);
        binding.itemTvOil.setMaxSize(10);

        binding.itemTvSeries.setOnWholeItemClickListener(this);
        binding.itemTvSeries.onTextChange(new ItemInputTextView.onTextChange() {
            @Override
            public void change(String s) {
                setTitleDefault();
            }
        });

        adapter = new ImageAdapter(this, null, null, 5);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(CarPublishActivity1.this, 4));
        binding.recyclerView.setAdapter(adapter);


        dictAdapter = new MultiDictAdapter4(CarPublishActivity1.this, null);
        binding.rlvList.setLayoutManager(new GridLayoutManager(CarPublishActivity1.this, 4));
        binding.rlvList.setAdapter(dictAdapter);
        dictAdapter.setMultiSelect(true);
        dictAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<DictEntity3>() {
            @Override
            public void onItemMultiClick(List<DictEntity3> dictEntity3s, int position) {
                additionIds.clear();
                for (DictEntity3 dictEntity3 : dictEntity3s) {
                    additionIds.add(dictEntity3.getId());
                }
                viewModel.addCarEntity.get().getAddCarStoreEntity3().setAdditionType(additionIds);
            }
        });
    }

    @Override
    protected void init() {
        fileList = new ArrayList<>();
        urls = new ArrayList<>();

        updateUserNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        myCompanyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        String myCompanyName = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME, "");
        String contact = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_NAME, "");
        String loginPhone = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, "");
        binding.itemTvDepartmentUnit.setContent(myCompanyName);
        binding.itemTvDepartmentPerson.setContent(contact);
        binding.itemTvContact.setContent(contact);
        binding.itemTvPhone.setContent(loginPhone);
        //企业用户获取申请 部门
        if (!TextUtils.isEmpty(myCompanyNo) && !"0".equals(myCompanyNo)) {
            viewModel.getDepartment();
            binding.itemTvDepartmentUnit.setVisibility(View.VISIBLE);
            binding.itemTvDepartmentName.setVisibility(View.VISIBLE);
            binding.itemTvDepartmentPerson.setVisibility(View.VISIBLE);
            isCompany = true;
            viewModel.addCarEntity.get().setCompany(isCompany);
            viewModel.addCarEntity.get().setMyCompanyNo(myCompanyNo);
            binding.itemTvName.setVisibility(View.VISIBLE);//企业用户显示门店名称
        }

        id = getIntent().getStringExtra("id");

        if (!TextUtils.isEmpty(id)) {
            viewModel.addCarEntity.get().setId(id);
            viewModel.getCarDetail(id);
            binding.topLayout.titleView.setText("车辆出租编辑");
        }
        viewModel.getCarAddition();
        viewModel.getLeaseRate();
        initInvoiceData();
    }

    //初始化发票选择数据
    private void initInvoiceData() {
        //是否选择发票（0:是;1:否）
        invoiceDatas = new ArrayList<>();
        invoiceDatas.add(new PickSelectEntity("1", "是"));
        invoiceDatas.add(new PickSelectEntity("0", "否"));
        //发票类型
        invoiceGGDatas = new ArrayList<>();
        invoiceGGDatas.add(new PickSelectEntity("0", "普通发票"));
        invoiceGGDatas.add(new PickSelectEntity("1", "专用发票"));

        //设置默认选择
        if (TextUtils.isEmpty(viewModel.addCarEntity.get().getNeedInvoice())) {//数据为空,代表新增
            binding.itemTvInvoice.setContent("是");
            binding.itemTvInvoiceGg.setContent("普通发票");
            Objects.requireNonNull(viewModel.addCarEntity.get()).setNeedInvoice("1");
            Objects.requireNonNull(viewModel.addCarEntity.get()).setInvoiceType("0");
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片结果回调

                // 图片、视频、音频选择结果回调
                selectList = PictureSelector.obtainMultipleResult(data);
                fileList.clear();
                urls.clear();
                for (LocalMedia localMedia : selectList) {
                    File file = new File(localMedia.getRealPath());
                    fileList.add(file);
                    urls.add(localMedia.getRealPath());
                }
                adapter.setDatas(selectList, urls);
                adapter.setFileList(fileList);
            }
        }
        if (data == null) {
            return;
        }
        if (requestCode == 251) {//取车详细地址
            double lat = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0);
            double lon = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0);
            String address = data.getStringExtra(SelectAddressActivity.CURRENT_POSITION);
            AddCarAddressEntity3 addCarAddressEntity3 = new AddCarAddressEntity3();
            addCarAddressEntity3.setLatitude(lat + "");
            addCarAddressEntity3.setLongitude(lon + "");
            addCarAddressEntity3.setAddressName(address);
            binding.itemTvGetcaraddress.setContent(address);
            viewModel.addCarEntity.get().getAddCarStoreEntity3().setPickupAddressEntity3(addCarAddressEntity3);
        } else if (requestCode == 252) {//还车详细地址
            double lat = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0);
            double lon = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0);
            String address = data.getStringExtra(SelectAddressActivity.CURRENT_POSITION);
            AddCarAddressEntity3 addCarAddressEntity3 = new AddCarAddressEntity3();
            addCarAddressEntity3.setLatitude(lat + "");
            addCarAddressEntity3.setLongitude(lon + "");
            addCarAddressEntity3.setAddressName(address);
            binding.itemTvBackcaraddress.setContent(address);
            viewModel.addCarEntity.get().getAddCarStoreEntity3().setReturnAddressEntity3(addCarAddressEntity3);
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_series://获取车系列表
                if (!TextUtils.isEmpty(binding.itemTvBrand.getContent())) {
                    viewModel.getVehicleSystem(viewModel.addCarEntity.get().getCarBrand());
                } else {
                    MToast.showToast(this, "请选择车辆品牌");
                }
                break;
            case R.id.item_tv_type://车型
                if (typeEntityList != null) {
                    showTypePicker(typeEntityList);
                } else {
                    viewModel.getPickerCarType();
                }
                break;
            case R.id.item_tv_brand://品牌
                CarBrandActivity.start(CarPublishActivity1.this);
                break;
            case R.id.item_tv_year_date://年代款
                if (yearEntityList != null) {
                    showYearPicker(yearEntityList);
                } else {
                    viewModel.getPickerCarYear();
                }
                break;
            case R.id.item_tv_num://座位数
                if (!TextUtils.isEmpty(viewModel.addCarEntity.get().getCarType())) {
                    viewModel.getPickerCarNum(viewModel.addCarEntity.get().getCarType());
                } else {
                    MToast.showToast(this, "请选择车型");
                }
                break;
            case R.id.item_tv_speed://变速箱
                if (speedEntityList != null) {
                    showSpeedPicker(speedEntityList);
                } else {
                    viewModel.getPickerCarSpeed();
                }
                break;
            case R.id.item_tv_config://燃油形式
                if (configEntityList != null) {
                    showConfigPicker(configEntityList);
                } else {
                    viewModel.getPickerCarConfig();
                }
                break;
            case R.id.item_tv_fuel://燃油标号
                if (TextUtils.isEmpty(viewModel.addCarEntity.get().getFuelType())) {
                    MToast.showToast(this, "请选择燃油形式");
                    return;
                }
                if ("4".equals(viewModel.addCarEntity.get().getFuelType())) {
                    viewModel.getFuelLabelSelect("1");
                } else {
                    viewModel.getFuelLabelSelect(viewModel.addCarEntity.get().getFuelType());
                }
                break;
            case R.id.item_tv_driver:
                if (driveEntityList != null) {
                    showDrivePicker(driveEntityList);
                } else {
                    viewModel.getPickerCarDrive();
                }
                break;
            case R.id.item_tv_window://天窗
                if (windowEntityList != null) {
                    showWindowPicker(windowEntityList);
                } else {
                    viewModel.getPickerCarWindow();
                }
                break;
            case R.id.item_tv_pdc:
                if (pdcEntityList != null) {
                    showPdcPicker(pdcEntityList);
                } else {
                    viewModel.getPickerCarPdc();
                }
                break;
            case R.id.item_tv_department_name://申请部门
                if (null != departmentData && departmentData.size() > 0) {
                    OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this,
                            (options1, options2, options3, v) -> {
                                String content = departmentData.get(options1).getProjectscale();
                                binding.itemTvDepartmentName.setContent(content);
                                viewModel.addCarEntity.get().setDepartmentNo(departmentData.get(options1).getNo());
                                viewModel.addCarEntity.get().setDepartmentName(content);
                            }).build();
                    businessOption.setPicker(departmentData);
                    businessOption.setSelectOptions(0);
                    businessOption.show();
                } else {
                    MToast.showToast(CarPublishActivity1.this, "暂无申请部门");
                }
                break;
            case R.id.item_tv_invoice://是否提供发票
                if (null != invoiceDatas && invoiceDatas.size() > 0) {
                    OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
                        String content = invoiceDatas.get(options1).getProjectscale();
                        binding.itemTvInvoice.setContent(content);
                        Objects.requireNonNull(viewModel.addCarEntity.get()).setNeedInvoice(invoiceDatas.get(options1).getNo());
                        if ("否".equals(content)) {
                            binding.itemTvInvoiceGg.setVisibility(View.GONE);
                            binding.itemTvSl.setVisibility(View.GONE);
                        } else {
                            binding.itemTvInvoiceGg.setVisibility(View.VISIBLE);
                            binding.itemTvSl.setVisibility(View.VISIBLE);
                        }
                    }).build();
                    businessOption.setPicker(invoiceDatas);
                    businessOption.setSelectOptions(0);
                    businessOption.show();
                }
                break;
            case R.id.item_tv_invoice_gg://发票类型
                if (null != invoiceGGDatas && invoiceGGDatas.size() > 0) {
                    OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
                        String content = invoiceGGDatas.get(options1).getProjectscale();
                        binding.itemTvInvoiceGg.setContent(content);
                        Objects.requireNonNull(viewModel.addCarEntity.get()).setInvoiceType(invoiceGGDatas.get(options1).getNo());
                    }).build();
                    businessOption.setPicker(invoiceGGDatas);
                    businessOption.setSelectOptions(0);
                    businessOption.show();
                }
                break;
            case R.id.item_tv_sl://税率
                if (null != tariffDatas && tariffDatas.size() > 0) {
                    OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
                        binding.itemTvSl.setContent(tariffDatas.get(options1).getProjectscale());
                        viewModel.addCarEntity.get().setTariff(tariffDatas.get(options1).getNo());
                    }).build();
                    businessOption.setPicker(tariffDatas);
                    businessOption.setSelectOptions(0);
                    businessOption.show();
                }
                break;
            case R.id.item_tv_allow_time://选择时间
                TimeRangePopWindow4 timeRangePopWindow4 = new TimeRangePopWindow4(getActivity());
                timeRangePopWindow4.setOnTimeCallBack(new TimeRangePopWindow4.OnTimeCallBack() {

                    @Override
                    public void result(long startTime, long endTime, String content) {
                        if (startTime < endTime) {
                            binding.itemTvAllowTime.setContent(content);
                            viewModel.addCarEntity.get().getAddCarStoreEntity3().setPickupAndReturnTime(Arrays.asList(String.valueOf(startTime), String.valueOf(endTime)));
                            timeRangePopWindow4.dismiss();
                        } else {
                            MToast.showToast(CarPublishActivity1.this, "结束时间不能小于开始时间");
                        }

                    }
                });
                timeRangePopWindow4.showAtLocation(binding.grayLayout, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                timeRangePopWindow4.setTitle("取还车时间");
                break;
            case R.id.item_tv_getcararea://选择取车地区
                if (pickupEntityList != null) {
                    showpickupAreaPop();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.item_tv_backcararea://选择还车地区
                if (backEntityList != null) {
                    showBackAreaPop();
                } else {
                    viewModel.getBackAreaModel();
                }
                break;
            case R.id.item_tv_getcaraddress://选择取车地址
                if (pickupLongLat == null) {
                    MToast.showToast(CarPublishActivity1.this, "请先选择取车地区");
                    return;
                }

                getLocationLL(pickupLongLat, 251);
                break;
            case R.id.item_tv_backcaraddress:////选择还车地址
                if (returnLongLat == null) {
                    MToast.showToast(CarPublishActivity1.this, "请先选择还车地区");
                    return;
                }
                getLocationLL(returnLongLat, 252);
                break;
            case R.id.item_tv_servicer://选择客服
                if (this.serviceEntities != null) {
                    getCustomerOrgDataS(serviceEntities);
                } else {
                    viewModel.getCustomerOrgDatas();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_save://暂存
                viewModel.addCarEntity.get().setState("0");//草稿
                validity();
                break;
            case R.id.btn_submit://提交
                viewModel.addCarEntity.get().setState("1");//默认上架
                validity();
                break;
            case R.id.iv_back://
                onBackPressed();
                break;
            case R.id.tv_use://使用推荐标题
                binding.include.itemTvTitleEdit.setEditContent(binding.include.tvTitleDefault.getText().toString());
                break;
            case R.id.item_tv_out://超时计费单位
                if (rentEntityList != null) {
                    showRentPicker(rentEntityList);
                } else {
                    viewModel.getPickercarRent();
                }
                break;
            default:
                break;
        }
    }

    //设置默认标题
    private void setTitleDefault() {
        String tvYear = binding.itemTvYearDate.getContent();
        if (!TextUtils.isEmpty(binding.itemTvYearDate.getContent())) {
            tvYear = binding.itemTvYearDate.getContent() + "年";
        }
        binding.include.tvTitleDefault.setText("出租" + binding.itemTvBrand.getContent() + binding.itemTvSeries.getContent()
                + tvYear + binding.itemTvSpeed.getContent() + binding.itemTvDriver.getContent() + binding.itemTvType.getContent());
        binding.include.rlLeaseTitle.setVisibility(View.VISIBLE);
    }

    private void validity() {
        if (TextUtils.isEmpty(binding.itemTvContact.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请输入联系人");
            return;
        }
        if (TextUtils.isEmpty(binding.itemTvPhone.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请输入联系电话");
            return;
        }
        if (!CheckUtils.isMobileNum(binding.itemTvPhone.getContent()) && !CheckUtils.IsTelephone(binding.itemTvPhone.getContent())) {
            Toast.makeText(this, "请输入正确的联系电话", Toast.LENGTH_SHORT).show();
            return;
        }
        if (binding.itemTvType.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请选择车型");
            return;
        }
        if (binding.itemTvBrand.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请选择品牌");
            return;
        }
        if (binding.itemTvSeries.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请输入车系");
            return;
        }
        if (binding.itemTvYearDate.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请选择年代款");
            return;
        }
        if (binding.itemTvNum.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请选择座位数");
            return;
        }
        if (binding.itemTvSpeed.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请选择变速箱");
            return;
        }
        if (binding.itemTvOil.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请输入排量");
            return;
        }
        if (binding.itemTvDriver.getContent().equals("")) {
            MToast.showToast(CarPublishActivity1.this, "请选择驱动方式");
            return;
        }
        if (!TextUtils.isEmpty(binding.itemTvConfig.getContent())) {
            if (TextUtils.isEmpty(binding.itemTvFuel.getContent()) && !"3".equals(viewModel.addCarEntity.get().getFuelType())) {
                MToast.showToast(CarPublishActivity1.this, "请选择燃油标号");
                return;
            }
        }
        if (binding.itemTvInvoice.getContent().equals("是")) {
            if (TextUtils.isEmpty(binding.itemTvSl.getContent())) {
                MToast.showToast(CarPublishActivity1.this, "请填写税率");
                return;
            }
        }
        if (TextUtils.isEmpty(binding.itemTvAllowTime.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请选择取还车时间");
            return;
        }
        if (TextUtils.isEmpty(binding.itemTvGetcararea.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请选择取车地区");
            return;
        }
        if (TextUtils.isEmpty(binding.itemTvGetcaraddress.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请选择取车详细地址");
            return;
        }
        if (binding.itemTvName.getVisibility() == View.VISIBLE) {
            if (TextUtils.isEmpty(binding.itemTvName.getContent())) {
                MToast.showToast(CarPublishActivity1.this, "请输入门店名称");
                return;
            }
        }
        if (TextUtils.isEmpty(binding.itemTvMoney.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请输入车辆押金");
            return;
        }
        if (Double.parseDouble(binding.itemTvMoney.getContent()) <= 0) {
            MToast.showToast(CarPublishActivity1.this, "请输入大于0的车辆押金");
            return;
        }
        if (TextUtils.isEmpty(binding.itemTvPay.getContent())) {
            MToast.showToast(CarPublishActivity1.this, "请输入日租金");
            return;
        }
        if (Double.parseDouble(binding.itemTvPay.getContent()) <= 0) {
            MToast.showToast(CarPublishActivity1.this, "请输入大于0的日租金");
            return;
        }
        if (!TextUtils.isEmpty(binding.itemTvWeek.getContent()) && Double.parseDouble(binding.itemTvWeek.getContent()) <= 0) {
            MToast.showToast(CarPublishActivity1.this, "请输入大于0的周租金");
            return;
        }
        if (!TextUtils.isEmpty(binding.itemTvMonth.getContent()) && Double.parseDouble(binding.itemTvMonth.getContent()) <= 0) {
            MToast.showToast(CarPublishActivity1.this, "请输入大于0的月租金");
            return;
        }
        if (!TextUtils.isEmpty(binding.itemTvYear.getContent()) && Double.parseDouble(binding.itemTvYear.getContent()) <= 0) {
            MToast.showToast(CarPublishActivity1.this, "请输入大于0的年租金");
            return;
        }
        if (!TextUtils.isEmpty(binding.itemTvOutContent.getText()) && Double.parseDouble(binding.itemTvOutContent.getText().toString()) <= 0) {
            MToast.showToast(CarPublishActivity1.this, "请输入大于0的超时计费价格");
            return;
        }
        if (TextUtils.isEmpty(binding.include.itemTvTitleEdit.getEditContent())) {
            MToast.showToast(this, "请输入标题");
            return;
        }

        if (adapter.getItemCount() <= 1) {
            MToast.showToast(CarPublishActivity1.this, "请上传图片");
            return;
        }

        uploadImgList.clear();
        if (adapter.getFileList().size() != 0) {
            currentImgNum = 0;
            uploadImgList.clear();
            imgTotal = adapter.getFileList().size();
            viewModel.uploadMutiFileModel(adapter.getFileList().get(currentImgNum), 0, currentImgNum + 1);
        } else {
            uploadImgSuccess(adapter.getUrls(), 1);
        }
    }

    //图片单张上传
    public void uploadRefresh(String url, int type) {
        uploadImgList.add(url);
        if (currentImgNum + 1 == imgTotal) {
            uploadImgSuccess(uploadImgList, type);
        } else {
            currentImgNum++;
            viewModel.uploadMutiFileModel(adapter.getFileList().get(currentImgNum), 0, currentImgNum + 1);
        }
    }

    //税率
    public void showRate(List<PickSelectEntity> list) {
        tariffDatas = list;
    }


    //图片上传成功回调
    public void uploadImgSuccess(List<String> urls, int type) {
        //拼接图片url
        List<String> urlList = new ArrayList<>();
        for (int i = 0; i < urls.size(); i++) {
            urlList.add(urls.get(i));
        }

        if (type == 0) {
            if (viewModel.addCarEntity.get().getPictures() != null) {
                viewModel.addCarEntity.get().getPictures().addAll(urlList);
                urlList.clear();
                urlList.addAll(viewModel.addCarEntity.get().getPictures());
            }
        }
        Objects.requireNonNull(viewModel.addCarEntity.get()).setPictures(urlList);
        currentImgNum = 0;
        onPublish();
    }

    private void onPublish() {
        Objects.requireNonNull(viewModel.addCarEntity.get()).setTitle(binding.include.itemTvTitleEdit.getEditContent()); //标题
        Objects.requireNonNull(viewModel.addCarEntity.get()).setContact(binding.itemTvContact.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).setContactPhone(binding.itemTvPhone.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).setOilConsume(binding.itemTvOil.getContent());//排量
        Objects.requireNonNull(viewModel.addCarEntity.get()).setCarCapacity(binding.itemTvCapacity.getContent());//油箱容量
        Objects.requireNonNull(viewModel.addCarEntity.get()).setRemark(binding.cevRemark.getEditContent()); //描述
        //租赁租金
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseDayMoney(binding.itemTvPay.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseWeekMoney(binding.itemTvWeek.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseMonthMoney(binding.itemTvMonth.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseYearMoney(binding.itemTvYear.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseCarMoney(binding.itemTvMoney.getContent());
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseOutMoney(binding.itemTvOutContent.getText().toString());
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setLeaseOtherNote(binding.cevOther.getEditContent());//其他费用
        Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().setContactStoreName(binding.itemTvName.getContent());//门店名称
        //兼容服务端,否则服务端会报错
        if (TextUtils.isEmpty(viewModel.addCarEntity.get().getDepartmentNo())) {
            Objects.requireNonNull(viewModel.addCarEntity.get()).setDepartmentNo("");
        }
        //企业用户判断是否需要审批
        if (viewModel.addCarEntity.get().isCompany() && !"0".equals(viewModel.addCarEntity.get().getState())) {
            viewModel.haveApprovalOrNot(viewModel.addCarEntity.get().getMyCompanyNo(), "carRelease");
        } else {
            viewModel.publishModel();
        }
    }

    public void showRentPicker(List<CommonPickerEntity3> rentEntityList) {
        this.rentEntityList = rentEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = rentEntityList.get(options1).getName();
            binding.itemTvOutRight.setText(content);
            rentPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setUnit(rentEntityList.get(options1).getId());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarLeaseEntity3().setUnitLabel(content);
        }).build();
        businessOption.setPicker(rentEntityList);
        businessOption.setSelectOptions(rentPosition);
        businessOption.show();
    }

    public void showTypePicker(List<CommonPickerEntity3> typeEntityList) {
        if (typeEntityList == null || typeEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.typeEntityList = typeEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = typeEntityList.get(options1).getName();
            binding.itemTvType.setContent(content);
            typePosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarType(typeEntityList.get(options1).getId());
            setTitleDefault();
            binding.itemTvNum.setContent("");
            binding.itemTvNum.setHint("请选择座位数");
        }).build();
        businessOption.setPicker(typeEntityList);
        businessOption.setSelectOptions(typePosition);
        businessOption.show();
    }

    public void showYearPicker(List<CommonPickerEntity3> yearEntityList) {
        if (yearEntityList == null || yearEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.yearEntityList = yearEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = yearEntityList.get(options1).getName();
            binding.itemTvYearDate.setContent(content);
            yearPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarYear(yearEntityList.get(options1).getId());
            setTitleDefault();
        }).build();
        businessOption.setPicker(yearEntityList);
        businessOption.setSelectOptions(yearPosition);
        businessOption.show();
    }

    public void showNumPicker(List<CommonPickerEntity3> numEntityList) {
        if (numEntityList == null || numEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.numEntityList = numEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = numEntityList.get(options1).getName();
            binding.itemTvNum.setContent(content);
            numPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setSeat(numEntityList.get(options1).getId());
            setTitleDefault();
        }).build();
        businessOption.setPicker(numEntityList);
        businessOption.setSelectOptions(numPosition);
        businessOption.show();
    }

    public void showSpeedPicker(List<CommonPickerEntity3> speedEntityList) {
        if (speedEntityList == null || speedEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.speedEntityList = speedEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = speedEntityList.get(options1).getName();
            binding.itemTvSpeed.setContent(content);
            speedPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarSpeed(speedEntityList.get(options1).getId());
            setTitleDefault();
//            binding.include.tvTitleDefault.setText(binding.itemTvType.getContent() + binding.itemTvBrand.getContent() + binding.itemTvSeries.getContent()
//                    + binding.itemTvYear.getContent() + binding.itemTvNum.getContent() + binding.itemTvSpeed.getContent());
        }).build();
        businessOption.setPicker(speedEntityList);
        businessOption.setSelectOptions(speedPosition);
        businessOption.show();
    }

    public void showConfigPicker(List<CommonPickerEntity3> configEntityList) {
        if (configEntityList == null || configEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.configEntityList = configEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = configEntityList.get(options1).getName();
            String id = configEntityList.get(options1).getId();
            binding.itemTvConfig.setContent(content);
            configPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setFuelType(configEntityList.get(options1).getId());
            if ("3".equals(id)) {
                binding.itemTvFuel.setVisibility(View.GONE);
                Objects.requireNonNull(viewModel.addCarEntity.get()).setFuelLabel("");
            } else {
                binding.itemTvFuel.setVisibility(View.VISIBLE);
            }
            binding.itemTvFuel.setContent("");
            binding.itemTvFuel.setHint("请选择燃料标号");
        }).build();
        businessOption.setPicker(configEntityList);
        businessOption.setSelectOptions(configPosition);
        businessOption.show();
    }

    public void showFuelLabelSelectPicker(List<CommonPickerEntity3> fuelList) {
        if (fuelList == null || fuelList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.fuelList = fuelList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = fuelList.get(options1).getName();
            binding.itemTvFuel.setContent(content);
            fuelPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setFuelLabel(fuelList.get(options1).getId());
        }).build();
        businessOption.setPicker(fuelList);
        businessOption.setSelectOptions(fuelPosition);
        businessOption.show();
    }

    public void showDrivePicker(List<CommonPickerEntity3> driveEntityList) {
        if (driveEntityList == null || driveEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.driveEntityList = driveEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = driveEntityList.get(options1).getName();
            binding.itemTvDriver.setContent(content);
            drivePosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarDriver(driveEntityList.get(options1).getId());
            setTitleDefault();
        }).build();
        businessOption.setPicker(driveEntityList);
        businessOption.setSelectOptions(drivePosition);
        businessOption.show();
    }

    public void showWindowPicker(List<CommonPickerEntity3> windowEntityList) {
        if (windowEntityList == null || windowEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.windowEntityList = windowEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = windowEntityList.get(options1).getName();
            binding.itemTvWindow.setContent(content);
            windowPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarWindow(windowEntityList.get(options1).getId());
        }).build();
        businessOption.setPicker(windowEntityList);
        businessOption.setSelectOptions(windowPosition);
        businessOption.show();
    }

    public void showPdcPicker(List<CommonPickerEntity3> pdcEntityList) {
        if (pdcEntityList == null || pdcEntityList.size() == 0) {
            MToast.showToast(this, R.string.no_data);
            return;
        }
        this.pdcEntityList = pdcEntityList;
        OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            String content = pdcEntityList.get(options1).getName();
            binding.itemTvPdc.setContent(content);
            pdcPosition = options1;
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarPdc(pdcEntityList.get(options1).getId());
        }).build();
        businessOption.setPicker(pdcEntityList);
        businessOption.setSelectOptions(pdcPosition);
        businessOption.show();
    }

    /**
     * 展示车系列表弹窗
     *
     * @param carPositionList
     */
    public void showVehicleSystemPicker(List<CommonPickerEntity3> carPositionList) {
        this.carPositionList = carPositionList;
        if (carPositionList.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
                String content = carPositionList.get(options1).getName();
                binding.itemTvSeries.setContent(content);
                pdcPosition = options1;
                Objects.requireNonNull(viewModel.addCarEntity.get()).setCarSeriesName(carPositionList.get(options1).getName());
                Objects.requireNonNull(viewModel.addCarEntity.get()).setCarSeries(carPositionList.get(options1).getId());
                setTitleDefault();
            }).build();
            businessOption.setPicker(carPositionList);
            businessOption.setSelectOptions(pdcPosition);
            businessOption.show();
        } else {
            MToast.showToast(this, R.string.no_data);
        }

    }

    public void getCustomerOrgDataS(List<ServiceEntity> serviceEntities) {
        this.serviceEntities = serviceEntities;
        if (null != serviceEntities && serviceEntities.size() > 0) {
            OptionsPickerView businessOption = new OptionsPickerBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
                String content = this.serviceEntities.get(options1).getName();
                binding.itemTvServicer.setContent(content);
                Objects.requireNonNull(viewModel.addCarEntity.get()).setContactsVo(this.serviceEntities.get(options1));
            }).build();
            businessOption.setPicker(this.serviceEntities);
            businessOption.setSelectOptions(0);
            businessOption.show();
        }
    }

    //  返回经纬度信息
    public void getLocationLL(MapLongAndLatEntity data, int requestCode) {
        if (!TextUtils.isEmpty(data.getLat())) {
            if (!TextUtils.isEmpty(data.getLng())) {
                Intent intent = new Intent();
                intent.setClass(this, SelectAddressActivity.class);
                intent.putExtra(SelectAddressActivity.TITLE, "详细地址");
                intent.putExtra(SelectAddressActivity.HINT, "请输入地址");
                intent.putExtra(SelectAddressActivity.SAVE_TEXT, "确定");
                intent.putExtra(SelectAddressActivity.LATITUDE, Double.parseDouble(data.getLat()));
                intent.putExtra(SelectAddressActivity.LONGTITUDE, Double.parseDouble(data.getLng()));
                startActivityForResult(intent, requestCode);
            }
        }
    }

    //申请部门
    public void showDepartment(List<PickSelectEntity> list) {
        departmentData = list;
    }

    public void showEdit(AddCarEntity3 addCarEntity3) {
        viewModel.addCarEntity.set(addCarEntity3);
        binding.include.itemTvTitleEdit.setEditContent(addCarEntity3.getTitle());
        binding.itemTvContact.setContent(addCarEntity3.getContact());
        binding.itemTvPhone.setContent(addCarEntity3.getContactPhone());
        binding.itemTvType.setContent(addCarEntity3.getCarTypeLabel());
        binding.itemTvBrand.setContent(addCarEntity3.getCarBrandLabel());
        binding.itemTvYearDate.setContent(addCarEntity3.getCarYear());
        binding.itemTvNum.setContent(addCarEntity3.getSeatName());
        binding.itemTvSpeed.setContent(addCarEntity3.getCarSpeedLabel());
        binding.itemTvConfig.setContent(addCarEntity3.getFuelTypeName());
        binding.itemTvDriver.setContent(addCarEntity3.getCarDriverLabel());
        binding.itemTvWindow.setContent(addCarEntity3.getCarWindowLabel());
        binding.itemTvPdc.setContent(addCarEntity3.getCarPdcLabel());
        binding.itemTvSeries.setContent(addCarEntity3.getCarSeriesName());
        if (TextUtils.isEmpty(viewModel.addCarEntity.get().getFuelLabelName())) {
            binding.itemTvFuel.setVisibility(View.GONE);
        } else {
            binding.itemTvFuel.setVisibility(View.VISIBLE);
            binding.itemTvFuel.setContent(viewModel.addCarEntity.get().getFuelLabelName());
        }

        if (Objects.requireNonNull(viewModel.addCarEntity.get()).getPictures().size() > 0) {
            adapter.setNetImgList(viewModel.addCarEntity.get().getPictures());
        }

        if (!TextUtils.isEmpty(viewModel.addCarEntity.get().getDepartmentName()) && updateUserNo.equals(viewModel.addCarEntity.get().getUpdateUserNo())) {
            binding.itemTvDepartmentName.setContent(viewModel.addCarEntity.get().getDepartmentName());
        } else {
            viewModel.addCarEntity.get().setDepartmentNo("");
            viewModel.addCarEntity.get().setDepartmentName("");
        }

        viewModel.addCarEntity.get().setCompany(isCompany);
        viewModel.addCarEntity.get().setMyCompanyNo(myCompanyNo);

        //设置门店名称
        if (viewModel.addCarEntity.get().getAddCarStoreEntity3() != null) {
            binding.itemTvName.setContent(viewModel.addCarEntity.get().getAddCarStoreEntity3().getContactStoreName());
        }

        if (viewModel.addCarEntity.get().getAddCarLeaseEntity3() != null) {
            if (!TextUtils.isEmpty(viewModel.addCarEntity.get().getAddCarLeaseEntity3().getUnitLabel())) {
                binding.itemTvOutRight.setText(viewModel.addCarEntity.get().getAddCarLeaseEntity3().getUnitLabel());
            }
        }

        //设置发票回显
        if ("1".equals(viewModel.addCarEntity.get().getNeedInvoice())) {
            binding.itemTvInvoice.setContent("是");
            binding.itemTvSl.setVisibility(View.VISIBLE);
            binding.itemTvSl.setContent(viewModel.addCarEntity.get().getTariff() + "%");
            binding.itemTvInvoiceGg.setVisibility(View.VISIBLE);
            if ("0".equals(viewModel.addCarEntity.get().getInvoiceType())) {
                binding.itemTvInvoiceGg.setContent("普通发票");
            } else {
                binding.itemTvInvoiceGg.setContent("专用发票");
            }
        } else if ("0".equals(viewModel.addCarEntity.get().getNeedInvoice())) {
            binding.itemTvInvoice.setContent("否");
            binding.itemTvInvoiceGg.setVisibility(View.GONE);
            binding.itemTvSl.setVisibility(View.GONE);
        } else {//如果没有没有数据,代表是新发布,默认开具发票
            binding.itemTvInvoice.setContent("是");
            binding.itemTvInvoiceGg.setContent("普通发票");
            Objects.requireNonNull(viewModel.addCarEntity.get()).setNeedInvoice("1");
            Objects.requireNonNull(viewModel.addCarEntity.get()).setInvoiceType("0");
        }

        if (!TextUtils.isEmpty(viewModel.addCarEntity.get().getAddCarLeaseEntity3().getUnitLabel())) {
            binding.itemTvOutRight.setText(viewModel.addCarEntity.get().getAddCarLeaseEntity3().getUnitLabel());
        }

        //取还车时间
        if (viewModel.addCarEntity.get().getAddCarStoreEntity3().getPickupAndReturnTime() != null
                && viewModel.addCarEntity.get().getAddCarStoreEntity3().getPickupAndReturnTime().size() > 0) {
            String pickTime = viewModel.addCarEntity.get().getAddCarStoreEntity3().getPickupAndReturnTime().get(0);
            String backTime = viewModel.addCarEntity.get().getAddCarStoreEntity3().getPickupAndReturnTime().get(1);
            binding.itemTvAllowTime.setContent(timeStamp2Date(pickTime, "HH:mm") + " - " + timeStamp2Date(backTime, "HH:mm"));
        }
        String pickupAddress = viewModel.addCarEntity.get().getAddCarStoreEntity3().getPickupAddressEntity3().getAddressName();
        binding.itemTvGetcaraddress.setContent(pickupAddress);
        String returnAddress = viewModel.addCarEntity.get().getAddCarStoreEntity3().getReturnAddressEntity3().getAddressName();
        binding.itemTvBackcaraddress.setContent(returnAddress);
        binding.itemTvGetcararea.setContent(viewModel.addCarEntity.get().getAddCarStoreEntity3().getPickupAreaEntity3().getAreaName());
        binding.itemTvBackcararea.setContent(viewModel.addCarEntity.get().getAddCarStoreEntity3().getReturnAreaEntity3().getAreaName());

        setTitleDefault();
    }

    //监听车辆品牌界面发送的选择结果
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LeaseCarBrandEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_CAR_BRAND_SELECT) {
            CarBrandEntity brandEntity = (CarBrandEntity) event.getMessageData();
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarBrand(brandEntity.getId());

            //选择品牌的时候重置车系数据
            binding.itemTvBrand.setContent(brandEntity.getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarSeriesName("");
            Objects.requireNonNull(viewModel.addCarEntity.get()).setCarSeries("");
            binding.itemTvSeries.setContent("");
            binding.itemTvSeries.setHint("请输入车系(必填)");
            setTitleDefault();
        }
    }


    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_DATA_BACK:
                    if (thread == null) {//如果已创建就不再重新创建子线程了
                        thread = new Thread(() -> {
                            // 子线程中解析省市区数据
                            initBackJsonData();
                        });
                        thread.start();
                    }
                    break;
                case MSG_LOAD_DATA:
                    if (thread == null) {//如果已创建就不再重新创建子线程了
                        thread = new Thread(() -> {
                            // 子线程中解析省市区数据
                            initJsonData();
                        });
                        thread.start();
                    }
                    break;
                case MSG_LOAD_SUCCESS:
                    showpickupAreaPop();
                    isLoaded = true;
                    thread = null;
                    break;
                case MSG_LOAD_BACK_SUCCESS:
                    showBackAreaPop();
                    backLoad = true;
                    thread = null;
                    break;
                case MSG_LOAD_FAILED:
                    break;
            }
        }
    };

    public void showAdditionType(List<DictEntity3> dictEntityList) {
        dictAdapter.setDatas(dictEntityList);
        dictAdapter.checkDefultId(viewModel.addCarEntity.get().getAddCarStoreEntity3().getAdditionType());
    }

    public void showPickupAreraPop
            (List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.pickupEntityList = datas;
        if (isLoaded) {
            showpickupAreaPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
        }
    }

    public void showpickupAreaPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaName().clear();
            String areaName;
            if (pickupOpt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = pickupOpt3.get(options1).get(options2).get(options3).getName();
                pickupCityPosition3 = options3;
                Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaEntity3().setDistrictName(areaName);
                Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaEntity3().setDistrictNo(pickupOpt3.get(options1).get(options2).get(options3).getNo());

                Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaName().add(areaName);
            }
            String content = pickupEntityList.get(options1).getName()
                    + pickupOpt2.get(options1).get(options2).getName()
                    + areaName;
            pickupCityPosition1 = options1;
            pickupCityPosition2 = options2;
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaEntity3().setProvinceName(pickupEntityList.get(options1).getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaEntity3().setProvinceNo(pickupEntityList.get(options1).getNo());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaEntity3().setCityName(pickupOpt2.get(options1).get(options2).getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaEntity3().setCityNo(pickupOpt2.get(options1).get(options2).getNo());

            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaName().add(pickupEntityList.get(options1).getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getPickupAreaName().add(pickupOpt2.get(options1).get(options2).getName());

            binding.itemTvGetcararea.setContent(content);

            viewModel.getPickupLocationModel(pickupEntityList.get(options1).getNo(), pickupOpt2.get(options1).get(options2).getNo(), pickupOpt3.get(options1).get(options2).get(options3).getNo());
        }).build();
        cityOption.setPicker(pickupEntityList, pickupOpt2, pickupOpt3);
        cityOption.setSelectOptions(pickupCityPosition1, pickupCityPosition2, pickupCityPosition3);
        cityOption.show();
    }

    public void showBackAreraPop
            (List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.backEntityList = datas;
        if (backLoad) {
            showBackAreaPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA_BACK);
        }
    }

    public void showBackAreaPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(CarPublishActivity1.this, (options1, options2, options3, v) -> {
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnupAreaName().clear();
            String areaName;
            if (backOpt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = backOpt3.get(options1).get(options2).get(options3).getName();
                backCityPosition3 = options3;
                Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnAreaEntity3().setDistrictName(areaName);
                Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnAreaEntity3().setDistrictNo(backOpt3.get(options1).get(options2).get(options3).getNo());

                Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnupAreaName().add(areaName);
            }
            String content = backEntityList.get(options1).getName()
                    + backOpt2.get(options1).get(options2).getName()
                    + areaName;
            backCityPosition1 = options1;
            backCityPosition2 = options2;
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnAreaEntity3().setProvinceName(backEntityList.get(options1).getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnAreaEntity3().setProvinceNo(backEntityList.get(options1).getNo());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnAreaEntity3().setCityName(backOpt2.get(options1).get(options2).getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnAreaEntity3().setCityNo(backOpt2.get(options1).get(options2).getNo());

            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnupAreaName().add(backEntityList.get(options1).getName());
            Objects.requireNonNull(viewModel.addCarEntity.get()).getAddCarStoreEntity3().getReturnupAreaName().add(backOpt2.get(options1).get(options2).getName());

            binding.itemTvBackcararea.setContent(content);

            viewModel.getReturLnocationModel(backEntityList.get(options1).getNo(), backOpt2.get(options1).get(options2).getNo(), backOpt3.get(options1).get(options2).get(options3).getNo());
        }).build();
        cityOption.setPicker(backEntityList, backOpt2, backOpt3);
        cityOption.setSelectOptions(backCityPosition1, backCityPosition2, backCityPosition3);
        cityOption.show();
    }

    private void initJsonData() {
        //城市列表
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
        for (int x = 0; x < pickupEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            if (addCarEntity3 != null && addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getProvinceName() != null && addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getProvinceName().equals(pickupEntityList.get(x).getName())) {
                pickupCityPosition1 = x;
            }
            pickupOpt2.add(pickupEntityList.get(x).getChildren());
            for (int i = 0; i < pickupEntityList.get(x).getChildren().size(); i++) {
                cityList.add(pickupEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (pickupEntityList.get(x).getChildren().get(i).getChildren() == null
                        || pickupEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(pickupEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            pickupOpt3.add(areaList);
        }
        if (addCarEntity3 != null) {
            if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getCityName() != null)
                for (int x2 = 0; x2 < pickupOpt2.size(); x2++) {
                    if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getCityName().equals(pickupEntityList.get(pickupCityPosition1).getChildren().get(x2).getName())) {
                        pickupCityPosition2 = x2;
                        break;
                    }
                }
            if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getDistrictName() != null)
                for (int x3 = 0; x3 < pickupOpt3.size(); x3++) {
                    if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getDistrictName().equals(pickupEntityList.get(pickupCityPosition1).getChildren().get(pickupCityPosition2).getChildren().get(x3).getName())) {
                        pickupCityPosition3 = x3;
                        break;
                    }
                }
        }
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    private void initBackJsonData() {
        //城市列表
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
        for (int x = 0; x < backEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            if (addCarEntity3 != null && addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getProvinceName() != null && addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getProvinceName().equals(backEntityList.get(x).getName())) {
                backCityPosition1 = x;
            }
            backOpt2.add(backEntityList.get(x).getChildren());
            for (int i = 0; i < backEntityList.get(x).getChildren().size(); i++) {
                cityList.add(backEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (backEntityList.get(x).getChildren().get(i).getChildren() == null
                        || backEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(backEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            backOpt3.add(areaList);
        }
        if (addCarEntity3 != null) {
            if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getCityName() != null)
                for (int x2 = 0; x2 < backOpt2.size(); x2++) {
                    if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getCityName().equals(backEntityList.get(backCityPosition1).getChildren().get(x2).getName())) {
                        backCityPosition2 = x2;
                        break;
                    }
                }
            if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getDistrictName() != null)
                for (int x3 = 0; x3 < backOpt3.size(); x3++) {
                    if (addCarEntity3.getAddCarStoreEntity3().getPickupAreaEntity3().getDistrictName().equals(backEntityList.get(backCityPosition1).getChildren().get(backCityPosition2).getChildren().get(x3).getName())) {
                        backCityPosition3 = x3;
                        break;
                    }
                }
        }
        mHandler.sendEmptyMessage(MSG_LOAD_BACK_SUCCESS);
    }

    public void showPickupLocation(MapLongAndLatEntity longLat) {
        this.pickupLongLat = longLat;
    }

    public void showReturnLocation(MapLongAndLatEntity longLat) {
        this.returnLongLat = longLat;
    }

    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (seconds.equals("0")) {
            return "00:00";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 0.暂存:保存成功
     * 1.需要审批:提交成功(发布前提示需要审批对话框)
     * 2.不需要审批:发布成功(发布前提示不需要审批对话框)
     */
    public void showSuccess() {
        if ("0".equals(viewModel.addCarEntity.get().getState())) {
            MToast.showToast(this, "保存成功");
        } else if (isApprove) {
            MToast.showToast(this, "提交成功");
        } else {
            MToast.showToast(this, "发布成功");
        }
    }

    /**
     * 提示审批对话框
     *
     * @param b
     */
    public void showPublishTip(boolean b) {
        isApprove = b;
        String msg = b ? "确认提交当前审批业务？" : "当前内容不需要审核，提交后即发布，确认提交？";
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("系统提示")
                .setMsg(msg)
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.publishModel();
                    }

                    @Override
                    public void onLeftClick() {
                        return;
                    }
                }).show();
    }

    @Override
    public void onBackPressed() {
        if (!TextUtils.isEmpty(id)) {
            finish();
        } else {
            new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                    .setTitle("提示")
                    .setMsg("你确定要返回上一步吗？返回后填写的数据将无法保存。")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            CarPublishActivity1.this.finish();
                        }

                        @Override
                        public void onLeftClick() {
                            return;
                        }
                    }).show();
        }
    }
}

